<?php

class xinh extends WebService {

    const MAX_TOP_IMAGES_SIZE = 15;
    const MAX_ALBUM_SIZE = 15;
    
    
    
    /**
     * actionDefault
     * @todo Tra ve random video theo tung ngay
    */  
    public function actionDefault() {
        $top_new_image = array();
        $status_code = 0; 
        import("database");
        /* ========================  TOP VIDEOS  ====================== */
            $top_new_video = array();
            if(!isset($_GET['page'])){
               $page = 1;
            }else{
               $page = $_GET['page'];
            }
            if(!is_numeric($page)){
               die('argument $page is not numberic');
            }else{
               $sql_query = "SELECT 
                                  xinh_images.id,
                                  xinh_images.link,
                                  xinh_images.date,
                                  xinh_images.alb_id,
                                  xinh_images.view,
                                  xinh_images.ilike,
                                  xinh_albums.name,
                                  xinh_albums.link_profile
                            FROM  
                                  xinh_images 
                            INNER JOIN
                                  xinh_albums
                            ON   
                                  xinh_albums.id = xinh_images.alb_id
                            WHERE
                                  xinh_images.active = 1   
                            AND
                                  xinh_images.hot = 1   
                            ORDER BY 
                                  xinh_images.id DESC
                            LIMIT " . ($page - 1) * self::MAX_TOP_IMAGES_SIZE . "," . self::MAX_TOP_IMAGES_SIZE;
      
      
              $db_access = new db_query($sql_query);
              while ($row = mysql_fetch_assoc($db_access->result)) {
                  $top_new_image[] = $row;
              }
              if(count($top_new_image) > 0){
                  $status_code = 1;
                  $message =  'Thành công';
              }
              mysql_free_result($db_access->result);
              unset($db_access);    
            }         
        

        /* ======================== RESPONSE  ================ */
        $response = new RequestResponse();
        $response->setBodyJSON(array("code" => $status_code ,"images" => $top_new_image));
        $response->process();
    }
    
    /**
     * actionDetailImage
     * @todo Tra ve random video theo tung ngay
    */  
    public function actionDetailImage() {        
        $status_code = 0; 
        import("database");
        /* ========================  GET INFO IMAGES ====================== */
            $array_info_image = array();
            $arr_cmt = array();
            if(!isset($_GET['id'])){
               die('argument $id is not empty');
            }else{
               $id = $_GET['id'];
               //Up view
               $db_exe = new db_execute('UPDATE
                                             xinh_images
                                          SET
                                             view = view + 1
                                          WHERE
                                             id =' . $id );
               unset($db_exe);
            }
            $sql_query = "SELECT 
                               link,
                               date,
                               alb_id,
                               view,
                               ilike
                         FROM  
                               xinh_images 
                         WHERE
                               active = 1
                         AND
                               id = " . $id . "        
                         ORDER BY 
                               id DESC
                         LIMIT 1 ";
   
              $db_access = new db_query($sql_query);
              while($row = mysql_fetch_assoc($db_access->result)){
                  $array_info_image[] = $row;
              }
              if(count($array_info_image) > 0){
                  $status_code = 1;
                  $message =  'Thành công';
                  
                  //Get commments
                  $sql_query = 'SELECT id,
                                       user_name,
                                       content,
                                       date
                                    FROM
                                       xinh_comments
                                    WHERE
                                       img_id = ' . $id;
                  $db_access = new db_query($sql_query);
                  while($row = mysql_fetch_assoc($db_access->result)){
                     $arr_cmt[] = $row;
                  }
              }
              mysql_free_result($db_access->result);
              unset($db_access);        
        
        /* ======================== RESPONSE  ================ */
        $response = new RequestResponse();
        $response->setBodyJSON(array("code" => $status_code ,"images" => $array_info_image,"comments" => $arr_cmt));
        $response->process();
    }
    
    
    /**
     * actionListAlbum
     * @todo Tra ve random video theo tung ngay
    */  
    public function actionlistAlbum() {
        $array_album = array();
        $list_ads    = array(); 
        $status_code = 0; 
        import("database");
        /* ========================  GET album ====================== */
            $top_new_video = array();
            if(!isset($_GET['page'])){
               $page = 1;
            }else{
               $page = $_GET['page'];
            }
            if(!isset($_GET['cat_id'])){
               $cat_id = 0;
            }else{
               $cat_id = $_GET['cat_id'];
            }
            if(!is_numeric($page)){
               die('argument $page is not numberic');
            }else{
               $sql_query = "SELECT 
                                  xinh_albums.id,
                                  name,
                                  total_images,
                                  xinh_images.link as avatar,
                                  xinh_images.id as img_id,
                                  link_profile,
                                  xinh_albums.date,
                                  xinh_albums.view,
                                  alike,
                                  cat_id
                            FROM  
                                  xinh_albums 
                            INNER JOIN
                                  xinh_images
                            ON
                                  xinh_albums.id = xinh_images.alb_id
                            WHERE
                                  xinh_albums.active = 1
                            AND
                                  xinh_images.is_avatar = 1  
                            AND
                                  xinh_albums.cat_id = ".$cat_id."
                            GROUP BY
                                  xinh_albums.id     
                            ORDER BY 
                                  id DESC
                            LIMIT " . ($page - 1) * self::MAX_ALBUM_SIZE . "," . self::MAX_ALBUM_SIZE;              
              $db_access = new db_query($sql_query);
              //echo $sql_query;
              while ($row = mysql_fetch_assoc($db_access->result)) {
                  $array_album[] = $row;
              }
              if(count($array_album) > 0){
                  $status_code = 1;
                  $message =  'Thành công';
                                    
              }
              mysql_free_result($db_access->result);
              unset($db_access);    
            }         
        

        /* ======================== RESPONSE  ================ */
        $response = new RequestResponse();
        $response->setBodyJSON(array("code" => $status_code ,"ads" => $list_ads, "albums" => $array_album));
        $response->process();
    }   
    
    /**
     * actionDetailAlbum
     * @todo Tra ve random video theo tung ngay
    */  
    public function actionDetailAlbum() {
        $array_list_image = array();
        $arr_cmt = array();
        $status_code = 0; 
        import("database");
        /* ========================  GET album ====================== */
            if(!isset($_GET['id'])){
               die('argument $id is not empty');
            }else{
               $id = $_GET['id'];
               //Up view
               $db_exe = new db_execute('UPDATE
                                             xinh_albums
                                          SET
                                             view = view + 1
                                          WHERE
                                             id =' . $id );
               unset($db_exe);
            }
            if(!isset($_GET['page'])){
               $page = 1;
            }else{
               $page = $_GET['page'];
            }
            if(!is_numeric($page)){
               die('argument $page is not numberic');
            }else{
               $sql_query = "SELECT 
                                  id,                                 
                                  link,
                                  date,
                                  view,
                                  ilike
                            FROM  
                                  xinh_images
                            WHERE
                                  active = 1
                            AND
                                  alb_id = " . $id . "      
                            ORDER BY 
                                  id DESC
                            LIMIT " . ($page - 1) * self::MAX_TOP_IMAGES_SIZE . "," . self::MAX_TOP_IMAGES_SIZE;
              echo $sql_query; 
              $db_access = new db_query($sql_query);
              while ($row = mysql_fetch_assoc($db_access->result)) {
                  $array_list_image[] = $row;
              }
              if(count($array_list_image) > 0){
                  $status_code = 1;
                  $message =  'Thành công';
                  
                  //Get commments
                  $sql_query = 'SELECT id,
                                       user_name,
                                       content,
                                       date
                                    FROM
                                       xinh_comments
                                    WHERE
                                       alb_id = ' . $id;
                  $db_access = new db_query($sql_query);
                  while($row = mysql_fetch_assoc($db_access->result)){
                     $arr_cmt[] = $row;
                  }
              }
              mysql_free_result($db_access->result);
              unset($db_access);
            }             
        
        /* ======================== RESPONSE  ================ */
        $response = new RequestResponse();
        $response->setBodyJSON(array("code" => $status_code ,"albums" => $array_list_image,"comments" => $arr_cmt));
        $response->process();
    }    
    
    /**
     * 
     * Comment image
    */
    public function actionCommentImage(){
      $status_code = 0;
      $message = 'error';
      if(!isset($_GET['img_id'])){
         die('$img_id not empty');
      }else{
         $img_id = $_GET['img_id'];
      }
      if(!isset($_GET['cmt_content'])){
         die('$cmt_content not empty');
      }else{
         $cmt_content = $_GET['cmt_content'];
      }
      if(!isset($_GET['user_name'])){
         die('$user_name not empty');
      }else{
         $user_name = $_GET['user_name'];
      }
      import("database");
      $sql_insert = "INSERT INTO 
                              xinh_comments(img_id,
                                            user_name,
                                            content,
                                            date)
                                       VALUES(" . $img_id . ",
                                             '" . $user_name . "',
                                             '" . $cmt_content . "',
                                              " . time() . ")";
      $db_exe = new db_execute($sql_insert);
      $row_inserted = $db_exe->total;
      if($row_inserted > 0){
         $status_code = 1;
         $message = 'Success';
      }
      
      /* ========================== RESPONSE ========================== */
      $response = new RequestResponse();
      $response->setBodyJSON(array("code" => $status_code ,"message" => $message));
      $response->process();
    }
    
    /**
     * 
     * Comment album
    */
    public function actionCommentAlbum(){
      $status_code = 0;
      $message = 'error';
      if(!isset($_GET['alb_id'])){
         die('$alb_id not empty');
      }else{
         $alb_id = $_GET['alb_id'];
      }
      if(!isset($_GET['cmt_content'])){
         die('$cmt_content not empty');
      }else{
         $cmt_content = $_GET['cmt_content'];
      }
      if(!isset($_GET['user_name'])){
         die('$user_name not empty');
      }else{
         $user_name = $_GET['user_name'];
      }
      import("database");
      $sql_insert = "INSERT INTO 
                              xinh_comments(alb_id,
                                            user_name,
                                            content,
                                            date)
                                       VALUES(" . $alb_id . ",
                                             '" . $user_name . "',
                                             '" . $cmt_content . "',
                                              " . time() . ")";
      $db_exe = new db_execute($sql_insert);
      $row_inserted = $db_exe->total;
      if($row_inserted > 0){
         $status_code = 1;
         $message = 'Success';
      }
      
      /* ========================== RESPONSE ========================== */
      $response = new RequestResponse();
      $response->setBodyJSON(array("code" => $status_code ,"message" => $message));
      $response->process();
    }
    
    /**
     * 
     * Report Image
    */
    public function actionReportImage(){
      $status_code = 0;
      $message = 'success';
      if(!isset($_GET['img_id'])){
         die('$img_id not empty');
      }else{
         $img_id = $_GET['img_id'];
      }
      import("database");
      $sql_sel = "SELECT link
                           FROM
                              xinh_images
                           WHERE id =" . $img_id;
      $db_sel = new db_query($sql_sel);
      if($row = mysql_fetch_assoc($db_sel->result)){
         $link = $row['link'];
         $status_code = get_http_response_code($link);
         if($status_code != 200){
            $db_update = new db_execute('UPDATE xinh_images SET active = 0 WHERE id =' . $img_id);
            $message = 'Deactive';
         }
      }
      
      /* ========================== RESPONSE ========================== */
      $response = new RequestResponse();
      $response->setBodyJSON(array("code" => $status_code ,"message" => $message));
      $response->process();
    }
    
}

?>
